package net.wanglu.www.zzz.rule.artcl;

import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.np._NPTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class Gulfs extends _ArticleRule {

	public Pattern GulfOf = Pattern.compile("\\bGulf of\\b");
	public Pattern Gulf = Pattern.compile("\\bGulf\\b");
	public Pattern Bay = Pattern.compile("\\bBay\\b");
	public Pattern BayOf = Pattern.compile("\\bBay of\\b");
	public Pattern GulfNoThe = Pattern.compile("\\bAmundsen Gulf\\b|\\bDavao Gulf\\b|\\bExmouth Gulf\\b|\\bGulf St Vincent\\b");
	public Pattern GulfOfNoThe = Pattern.compile("\\bGulf of the Farallones\\b|\\bGulf of Hammamet\\b|\\bGulf of Odessa\\b|\\bGulf of Sidra\\b|\\bGulf of Tunis\\b");

	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		if (NP.depth() != 2)
			return Rule.Inapplicable;// 如果不是叶子节点的父母则不符合要求
		TaggedWord headword = npt.getHeadword();
		if (headword.tag().startsWith("NN")) {
			return Rule.Applicable;// 如果中心词是名词则符合要求，否则不符合
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if (this.applicable(objects) == Rule.Inapplicable)
			return Rule.Inapplicable;

		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();

		TregexPattern pattern = null;
		TregexMatcher matcher = null;

		/*
		 * 名词短语中含有的冠词，也可能没有
		 */
		sb.clear();
		sb.append("/\\bDT\\b/>,/\\bNP\\b/");
		pattern = TregexPattern.compile(sb.toString());
		matcher = pattern.matcher(NP);

		/*
		 * 名词短语中
		 */
		sb.clear();
		for (TaggedWord tw : NP.taggedYield()) {
			sb.append(tw.word()).append(" ");
		}
		
		if (this.GulfOf.matcher(sb.toString()).find()) {
			if ( this.GulfOfNoThe.matcher(sb.toString()).find()) {
				if (matcher.find()) {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					return egg;
				} else {
					return null;
				}				
			} else {
				if (sb.toString().trim().startsWith("the")
						|| sb.toString().trim().startsWith("The")) {
					return null;
				} else {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					return egg;
				}				
			}
		} else if (this.Gulf.matcher(sb.toString()).find()) {
			if ( this.GulfNoThe.matcher(sb.toString()).find()) {
				if (matcher.find()) {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					return egg;
				} else {
					return null;
				}				
			} else {
				if (sb.toString().trim().startsWith("the")
						|| sb.toString().trim().startsWith("The")) {
					return null;
				} else {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setViolatedRule(this);
					return egg;
				}				
			}
		} else if (this.BayOf.matcher(sb.toString()).find()) {
			if (sb.toString().trim().startsWith("the")
					|| sb.toString().trim().startsWith("The")) {
				return null;
			} else {
				BadEgg egg = new BadEgg();
				egg.whereIsWrong = NP;
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				return egg;
			}				
		} else if (this.Bay.matcher(sb.toString()).find()) {
			if (matcher.find()) {
				BadEgg egg = new BadEgg();
				egg.whereIsWrong = NP;
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				return egg;
			} else {
				return null;
			}				
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.append("<p>");
		sb.append("如果表达海湾使用Gulf、Gulf of、Bay of这些名词，一般前面要加定冠词the。");
		sb.append("<br>");
		sb.append("但也有例外，下面描述海湾的短语不加定冠词：Amundsen Gulf、Davao Gulf、Exmouth Gulf、Gulf of the Farallones、Gulf of Hammamet、Gulf of Odessa、Gulf St Vincent、Gulf of Sidra、Gulf of Tunis");
		sb.append("<br>");
		sb.append("如果表达海湾只使用Bay，一般前面不用加定冠词。比如：Passamaquoddy Bay、Algoa Bay。");
		sb.append("</p>");
		return sb.toString();
	}
}
